package itqf.dao;

import itqf.dao.impl.ProductDao;
import itqf.entity.GoodTypes;
import itqf.entity.Product;
import itqf.utils.DruidUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * com.itqf.dao
 * 2020/9/15
 */
public class ProductDaoImpl implements ProductDao {
    QueryRunner qr = new QueryRunner(DruidUtils.getDateSource());

    @Override
    public List<Product> showAllProduct() {
        String sql = "select p_id as pid,p_name as pname,p_price as pprice,p_time as ptime,p_info as pinfo,p.t_id as tid,t_name as tname  from product p inner join type t on p.t_id=t.t_id";
        try {
            List<Map<String, Object>> map = qr.query(sql, new MapListHandler());
            List<Product> products = new ArrayList<>();
            for (Map<String, Object> list : map) {
                Product product = new Product();
                GoodTypes goodTypes = new GoodTypes();
                BeanUtils.populate(product,list);
                BeanUtils.populate(goodTypes,list);
                product.setTname(goodTypes.getTname());
                product.setTid(goodTypes.getTid());
                products.add(product);
            }
            return products;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long findTotalProductByTypeId(int typeId) {
        String sql = "select count(*) from product where t_id=?";
        try {
            Long count =(Long) qr.query(sql, new ScalarHandler(), typeId);
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Product> findProductByTypeId(int startIndex, int pageSize, int typeId) {
        String sql = "select p_id as pid,p_name as pname" +
                ",p_price as pprice,p_image as pimage ," +
                "p_info as pinfo,p_time as ptime,p_state as pstate from product where t_id=? limit ?,?";
        try {
            return qr.query(sql,new BeanListHandler<>(Product.class),typeId,startIndex,pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Product findProductById(int id) {
        String sql = "select p_id as pid,p_name as pname,p_price as pprice,p_image as pimage," +
                "p_info as pinfo,p_time as ptime,p_state as pstate,t.t_id as tid,t.t_name as tname" +
                " from product p inner join type t on p.t_id=t.t_id where p.p_id=?";
        //String sql2 = "select P.*,t.* from product p,type t where p.t_id=t.t_id";
        try {
            Product product = qr.query(sql,new BeanHandler<>(Product.class),id);
            return product;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Product> showGoodsType() {
        String sql = "select p_name as pname,p_state as pstate,p.t_id as tid,t_name as tname from product p inner join type t on p.t_id=t.t_id";
        try {
            List<Map<String, Object>> map = qr.query(sql, new MapListHandler());
            List<Product> products = new ArrayList<>();
            for (Map<String, Object> list : map) {
                Product product = new Product();
                GoodTypes goodTypes = new GoodTypes();
                BeanUtils.populate(product,list);
                BeanUtils.populate(goodTypes,list);
                product.setTname(goodTypes.getTname());
                product.setTid(goodTypes.getTid());
                products.add(product);
            }
            return products;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //添加商品
    @Override
    public void insert(String typename) {

    }


}
